import axios from 'axios'
import { Promise } from 'core-js'
import { reduce } from 'core-js/core/array'
import { reject } from 'core-js/fn/promise'
export default {
  state () {
    return {
      list: []
    }
  },
  mutations: {
    fn (state, data) {
      state.list = data
    },
    fg (state, obj) {
      let neObj = state.list.find((item) => item.id == obj.id)
      neObj.count = obj.count
    }
  },
  actions: {
    async GetList (aa) {
      const res = await axios.get('http://localhost:3000/cart')
      aa.commit('fn', res.data)
    },
    async syncCount ({ commit }, obj) {
      // const res = await axios.patch('http://localhost:3000/cart/' + obj.id, {
      //   count: obj.count
      // }
      // )
      // commit('fg', obj)
      new Promise(async (resolve, reject) => {
        const res = await axios.patch('http://localhost:3000/cart/' + obj.id, {
          count: obj.count
        })
      })
    }
  },
  getters: {
    // ff (state) {
    //   return state.list.reduce((item, sum) => item.count + sum, 0)
    // },
    // fff (state) {
    //   return state.list.reduce((item, sum) => item.count * item.price + sum, 0)
    // }
  },
  namespaced: true
}